﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Common
{
    public partial class SLSplitter : UserControl
    {
        bool m_bDown = false;
        bool m_bVertical = false;
        Point m_stPt = new Point();

        public Grid Grid1 = null;
        public Grid spliterr = null;
        public Grid Grid2 = null;

        public SLSplitter(bool bVertical)
        {
            InitializeComponent();
            InitSplitter(bVertical);
        }

        private void InitSplitter(bool bVertical)
        {
            m_bVertical = bVertical;
            MainGrid.MouseMove += new MouseEventHandler(MainGrid_MouseMove);
            MainGrid.MouseLeftButtonUp += new MouseButtonEventHandler(MainGrid_MouseLeftButtonUp);

            if (m_bVertical)
            {
                ColumnDefinition coldef = new ColumnDefinition();
                MainGrid.ColumnDefinitions.Add(coldef);

                coldef = new ColumnDefinition();
                coldef.Width = new GridLength(4);
                MainGrid.ColumnDefinitions.Add(coldef);

                coldef = new ColumnDefinition();
                MainGrid.ColumnDefinitions.Add(coldef);

                Grid1 = new Grid();
                Grid1.Background = new SolidColorBrush(Colors.Transparent);
                Grid1.HorizontalAlignment = HorizontalAlignment.Stretch;
                Grid1.VerticalAlignment = VerticalAlignment.Stretch;
                this.MainGrid.Children.Add(Grid1);
                Grid.SetColumn(Grid1, 0);


                spliterr = new Grid();
                spliterr.Background = new SolidColorBrush(Colors.LightGray);
                spliterr.HorizontalAlignment = HorizontalAlignment.Stretch;
                spliterr.VerticalAlignment = VerticalAlignment.Stretch;
                spliterr.MouseLeftButtonDown += new MouseButtonEventHandler(spliterr_MouseLeftButtonDown);
                spliterr.MouseEnter += new MouseEventHandler(spliterr_MouseEnter);
                spliterr.MouseLeave += new MouseEventHandler(spliterr_MouseLeave);

                this.MainGrid.Children.Add(spliterr);
                Grid.SetColumn(spliterr, 1);

                Grid2 = new Grid();
                Grid2.Background = new SolidColorBrush(Colors.Transparent);
                Grid2.HorizontalAlignment = HorizontalAlignment.Stretch;
                Grid2.VerticalAlignment = VerticalAlignment.Stretch;
                this.MainGrid.Children.Add(Grid2);
                Grid.SetColumn(Grid2, 2);

            }
            else
            {
                RowDefinition rowdef = new RowDefinition();
                //rowdef.Height = GridLength.Auto;
                MainGrid.RowDefinitions.Add(rowdef);

                rowdef = new RowDefinition();
                rowdef.Height = new GridLength(4, GridUnitType.Pixel);
                MainGrid.RowDefinitions.Add(rowdef);

                rowdef = new RowDefinition();
                //rowdef.Height = GridLength.Auto;
                MainGrid.RowDefinitions.Add(rowdef);


                Grid1 = new Grid();
                Grid1.Background = new SolidColorBrush(Colors.Transparent);
                Grid1.HorizontalAlignment = HorizontalAlignment.Stretch;
                Grid1.VerticalAlignment = VerticalAlignment.Stretch;
                this.MainGrid.Children.Add(Grid1);
                Grid.SetRow(Grid1, 0);

                spliterr = new Grid();
                spliterr.Background = new SolidColorBrush(Colors.LightGray);
                spliterr.HorizontalAlignment = HorizontalAlignment.Stretch;
                spliterr.VerticalAlignment = VerticalAlignment.Stretch;
                spliterr.MouseLeftButtonDown += new MouseButtonEventHandler(spliterr_MouseLeftButtonDown);
                spliterr.MouseEnter += new MouseEventHandler(spliterr_MouseEnter);
                spliterr.MouseLeave += new MouseEventHandler(spliterr_MouseLeave);

                this.MainGrid.Children.Add(spliterr);
                Grid.SetRow(spliterr, 1);

                Grid2 = new Grid();
                Grid2.Background = new SolidColorBrush(Colors.Transparent);
                Grid2.HorizontalAlignment = HorizontalAlignment.Stretch;
                Grid2.VerticalAlignment = VerticalAlignment.Stretch;
                this.MainGrid.Children.Add(Grid2);
                Grid.SetRow(Grid2, 2);
            }
        }

        void MainGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            m_bDown = false;
        }

        void MainGrid_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_bDown)
            {
                if (m_bVertical)
                {

                    ColumnDefinition def1 = MainGrid.ColumnDefinitions[0];
                    ColumnDefinition def2 = MainGrid.ColumnDefinitions[2];
                    Point curPt = e.GetPosition(MainGrid);
                    def1.Width = new GridLength(curPt.X, GridUnitType.Pixel);
                }
                else
                {
                    RowDefinition def1 = MainGrid.RowDefinitions[0];
                    RowDefinition def2 = MainGrid.RowDefinitions[2];
                    Point curPt = e.GetPosition(MainGrid);
                    def1.Height = new GridLength(curPt.Y, GridUnitType.Pixel);

                }
            }
        }

        private void spliterr_MouseEnter(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.IBeam;
        }

        private void spliterr_MouseLeave(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Arrow;
        }

        private void spliterr_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Point pt = e.GetPosition(spliter);

            m_bDown = true;
            m_stPt = e.GetPosition(MainGrid);
        }
    }
}
